const router=require("koa-router")()
const query=require('../utils/query');
const preApi = '/api/users';

const addRegister=(data)=>{
  console.log(data.phone)
  return new Promise((resolve,reject)=>{
    let search_sql=`select * from register where phone =${data.phone}`
    query(search_sql,function(res){
      console.log(res)
      if(res.length==0){
        let add_sql=`insert into register (phone,username,password) values (${data.phone},${data.username},${data.password})`;
        query(add_sql,function(res){
          resolve({
            error:0,
            message:"注册成功"
          })
        },function(err){
          console.log(err)
          resolve({
            error:1,
            message:"注册失败",
            data:err
          })
        })
      }else{
        resolve({
          error:1,
          message:"该手机号已被注册"
        })
      }
    },function(err){
      resolve({
        error:"500",
        message:"请求失败",
        data:err
      })
    })
  })
}
router
.post(`/signup`,async(ctx,next)=>{
  console.log(ctx.request.body.phone)
  let data=await addRegister(ctx.request.body);
  ctx.body=data;
})
module.exports=router;